package mylab.starters.oss.support.fastdfs;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import mylab.starters.oss.AbstractOssTemplate;
import mylab.starters.oss.OssProvider;
import mylab.starters.oss.OssRule;
import mylab.starters.oss.config.OssProperties;
import mylab.starters.oss.model.OssFile;
import mylab.starters.oss.model.OssFileMetaInfo;
import mylab.utils.spring.spi.SpiProvider;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class FastdfsTemplate extends AbstractOssTemplate<OssProperties.FastdfsProperties> {

    private final FastFileStorageClient storageClient;

    public FastdfsTemplate(OssProperties.FastdfsProperties properties, OssRule ossRule, FastFileStorageClient storageClient) {
        super(properties, ossRule);
        this.storageClient = storageClient;
    }

    @Override
    public SpiProvider getProvider() {
        return OssProvider.Fastdfs;
    }

    @Override
    public void createBucket(String bucketName) {

    }

    @Override
    public void deleteBucket(String bucketName) {

    }

    @Override
    public boolean bucketExists(String bucketName) {
        return false;
    }

    @Override
    public void copyFile(String sourceBucketName, String fileName, String targetBucketName) {

    }

    @Override
    public void copyFile(String sourceBucketName, String fileName, String targetBucketName, String targetFileName) {

    }

    @Override
    public OssFileMetaInfo getFileMetaInfo(String fileName) {
        return null;
    }

    @Override
    public OssFileMetaInfo getFileMetaInfo(String bucketName, String fileName) {
        return null;
    }

    @Override
    public String getFilePath(String fileName) {
        return null;
    }

    @Override
    public String getFilePath(String bucketName, String fileName) {
        return null;
    }

    @Override
    public String getFileLink(String fileName) {
        return null;
    }

    @Override
    public String getFileLink(String bucketName, String fileName) {
        return null;
    }

    @Override
    public OssFile uploadFile(MultipartFile file) {
        try {
            StorePath storePath = storageClient.uploadFile(
                    file.getInputStream(),
                    file.getSize(),
                    FilenameUtils.getExtension(file.getOriginalFilename()),
                    null);

            OssFile ossFile = new OssFile();
            ossFile.setName(storePath.getPath());
            ossFile.setOriginalName(storePath.getFullPath());
            ossFile.setLink(getResAccessUrl(storePath));
            return ossFile;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public OssFile uploadFile(String fileName, MultipartFile file) {
        return null;
    }

    @Override
    public OssFile uploadFile(String bucketName, String fileName, MultipartFile file) {
        return null;
    }

    @Override
    public OssFile uploadFile(String fileName, InputStream stream) {
        return null;
    }

    @Override
    public OssFile uploadFile(String bucketName, String fileName, InputStream stream) {
        return null;
    }

    @Override
    public void deleteFile(String fileName) {
        StorePath storePath = StorePath.parseFromUrl(fileName);
        storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
    }

    @Override
    public void deleteFile(String bucketName, String fileName) {

    }

    @Override
    public void deleteFiles(List<String> fileNames) {

    }

    @Override
    public void deleteFiles(String bucketName, List<String> fileNames) {

    }

    private String getResAccessUrl(StorePath storePath) {
        return "http://127.0.0.1:8080" + "/" + storePath.getFullPath();
    }
}
